package com.prs.crm.dao.exchange.impl;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.exchange.OnlinePersonDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.exchange.OnlinePerson;
import com.prs.crm.domain.hr.Person;
@SuppressWarnings("unchecked")
public class OnlinePersonDaoImpl extends BaseDaoImpl<OnlinePerson> implements OnlinePersonDao {

	
	public OnlinePerson findOnline(final Person person,final  String ip)
	{
		 return (OnlinePerson)this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(OnlinePerson.class);
				        	criteria.add(Restrictions.eq("oper", person))
				        					.add(Restrictions.eq("ip", ip));
				        	List<?> result =  criteria.list();
				        	return (result.size()>0)?result.get(0):null;
				        }   
					}
			);
	}
	
	
	public int removeOverTime(final Date limitTime)
	{
		 return (Integer)this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	String hql = "delete from OnlinePerson where updateTime<?";
				        	Query query = session.createQuery(hql);
				        	query.setParameter(0, limitTime);
				        	return query.executeUpdate();
				        }   
					}
			);
	}

	@Override
	public Integer countOnlinePersons() {
		// TODO Auto-generated method stub
		return (Integer)this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(OnlinePerson.class);
//				criteria.setProjection(Projections.rowCount());
				criteria.setProjection(Projections.groupProperty("ip"));
				List list = criteria.list();
				if(list.size() > 0){
					
					return list.size();
				}else{
				return 0;
				}
			}
		});
	}


	@Override
	public int removeOnlinePerson(final Integer id) {
		 return (Integer)this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	String hql = "delete from OnlinePerson where id=?";
				        	Query query = session.createQuery(hql);
				        	query.setParameter(0, id);
				        	return query.executeUpdate();
				        }   
					}
			);
	}


	@Override
	public Collection<OnlinePerson> getAll() {
		return (Collection<OnlinePerson>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(OnlinePerson.class);
				return criteria.list();
			}
			
		});
	}


	@Override
	public int removePerson(final Person person) {
		return (Integer)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{
					public Object doInHibernate(Session session) throws HibernateException {
			        	String hql = "delete from OnlinePerson where oper=?";
			        	Query query = session.createQuery(hql);
			        	query.setParameter(0, person);
			        	return query.executeUpdate();
			        }   
				}
		);
	}
}
